
package com.tbvtc.agriculturalcreditchainserver2025.Json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Component
public class TimesTampSerialized extends JsonSerializer<String> {

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        try {
            // 假设时间戳是以毫秒为单位的字符串
            long timestamp = Long.parseLong(s);
            LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
            jsonGenerator.writeString(dateTime.toString());
        } catch (NumberFormatException e) {
            // 处理时间戳格式错误的情况
            jsonGenerator.writeString("Invalid timestamp");
        }
    }
}